package com.pay.sdk;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import com.switfpass.pay.MainApplication;
import com.switfpass.pay.activity.PayPlugin;
import com.switfpass.pay.bean.RequestMsg;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

public class WXPayActivity extends Activity {
    private static final String TOKEN_ID = "token_id";
    private static final String APPID = "appid";
    private static final String PAY_JSON = "pay_json";
    private static final String EMPTY = "";
    private String mAppId;
    private String mToken;
    private String mPayJson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        mToken = bundle.getString(TOKEN_ID, EMPTY);
        mAppId = bundle.getString(APPID, EMPTY);
        if (TextUtils.isEmpty(mToken)) {
            mPayJson = bundle.getString(PAY_JSON, EMPTY);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!TextUtils.isEmpty(mToken)) {
            pay();
        } else {
            payNanYue();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    private void pay() {
        RequestMsg msg = new RequestMsg();
        PayLogUtil.log("WXPayActivity mAppId = " + mAppId + " mToken = " + mToken);
        msg.setTokenId(mToken);
        msg.setTradeType(MainApplication.WX_APP_TYPE);
        msg.setAppId(mAppId);
        PayPlugin.unifiedAppPay(WXPayActivity.this, msg);
    }

    private void payNanYue() {
        if (!TextUtils.isEmpty(mPayJson)) {
            try {
                JSONObject jsonObject = new JSONObject(mPayJson);
                String appId = jsonObject.getString("appid");
                String nonceStr = jsonObject.getString("noncestr");
                String prepayId = jsonObject.getString("prepayid");
                String partnerId = jsonObject.getString("partnerid");
                String packageValue = jsonObject.getString("package");
                String sign = jsonObject.getString("sign");
                String timeStamp = jsonObject.getString("timestamp");

                IWXAPI iwxapi = WXAPIFactory.createWXAPI(this, appId);
                iwxapi.registerApp(appId);
                PayReq payReq = new PayReq();
                payReq.appId = appId;
                payReq.nonceStr = nonceStr;
                payReq.prepayId = prepayId;
                payReq.partnerId = partnerId;
                payReq.packageValue = packageValue;
                payReq.sign = sign;
                payReq.timeStamp = timeStamp;
                iwxapi.sendReq(payReq);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
